package com.iflytek.hive.step2amountgoods;


import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class AmountGoodsReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
    private DoubleWritable valueOut = new DoubleWritable();
    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context)throws IOException, InterruptedException {
        //1:遍历集合求和
        double amount = 0;
        for (DoubleWritable value : values) {
            amount += value.get();
        }
        //2:将数据封装到对象中
        valueOut.set(Double.valueOf(amount));
        //3:将K3写入上下文中
        context.write(key, valueOut);
    }
}

